'use strict';

var clientUtils = angular.module('clientUtils');

clientUtils.factory('Util', [function(){      //TODO:
    return {

        getScreenSize: function() {
            var sh = window.screen.height;
            if (document.body.clientHeight < sh) {
                sh = document.body.clientHeight;
            }

            var sw = window.screen.width;
            if (document.body.clientWidth < sw) {
                sw = document.body.clientWidth;
            }
            return {h: sh, w:sw};
        },
        computerElemHeight: function(val, base) {
            var me = this;
            var rt;
            if (!val) {
                return base;
            }
            if (me.endWith(val, '%')) {
                rt = val.substring(0, val.length - 1) * 0.01 * base + 'px';
            } else {
                if (!me.endWith(val, 'px')) {
                    rt = val + 'px';
                } else {
                    rt = val;
                }
            }
            return rt;
        },
        endWith: function(str, suffix) {
            return str.indexOf(suffix, str.length - suffix.length) !== -1;
        },
        findInArray: function(arr, id) {
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].id == id) {
                    return arr[i];
                }
            }
            return undefined;
        }
    }
}]);

clientUtils.factory('StringUtil', [function(){
    return {
        isEmpty: function (o){
            if (o === null || o === "null" || o === undefined || o === "undefined" || o === "") {
                return true;
            } else {
                return false;
            }
        }
    }
}]);

clientUtils.factory('WidgetUtil', [ function(){
    return {
        convert: function (vo, map){
            var me = this;
            var res = angular.copy(vo);

            for(var left in map){
                var right = eval('map.' + left);
                var oldVo;
                if (res.data) {
                    oldVo = res.data
                } else {
                    oldVo = res;
                }
                if (Object.prototype.toString.call(oldVo) == '[object Array]') {
                    angular.forEach(oldVo, function(record) {
                        me.setData(record, left, right);
                    });
                } else {
                    me.setData(oldVo, left, right);
                }

            }
            return res;
        },
        setData: function (record, left, right){
            if (Object.prototype.toString.call(right) == '[object Function]') {
                eval('record.'+ left + '= (' + right + ')( record )');
            } else {
                eval('record.'+ left + '=' + right);
            }
        }
    }
}]);

clientUtils.factory('iScrollUtil', [function(){
    return {
        initIscroll: function(scope, element) {
            var me = this;

            scope.myScroll = new IScroll($(element).find('.wrapper')[0], {
                scrollX: false,
                scrollY: true,
                mouseWheel: true,
                click: true
            });
            scope.myScroll.on('scrollMove', function() {
                var $pullDown = $(element).find('.pulldown');
                var $pullUp = $(element).find('.pullup');

                if (this.y > 60) {
                    $pullDown.addClass('flip').find('.label').html('Release to refresh...');
                    scope.action = 'reset';
                } else {
                    $pullDown.removeClass('flip').find('.label').html('Pull down to refresh...');
                }

                if (this.maxScrollY - this.y > 60) {
                    if(!scope.noMore) {
                        $pullUp.addClass('flip').find('.label').html('Release to load more...');
                        scope.action = 'next_page';
                    } else {
                        scope.action = undefined;
                    }
                } else {
                    $pullUp.removeClass('flip').find('.label').html('Pull up to load more...');
                }
            });
            scope.myScroll.on('scrollEnd', function() {
                console.log('scrollEnd');
                var $wrapper = $(element).find('.wrapper');
                var $pullDown = $(element).find('.pulldown');
                var $pullUp = $(element).find('.pullup');

                if ($pullDown.hasClass('flip')) {
                    $wrapper.addClass('pulldownrefresh');
                    this.refresh();
                    $pullDown.removeClass('flip').addClass('loading').find('.label').html('Loading...');
                    me.pullDownAction(scope, $wrapper);
                }
                if(!scope.noMore) {
                    if ($pullUp.hasClass('flip')) {
                        $pullUp.removeClass('flip').addClass('loading').find('.label').html('Loading...');
                        me.pullUpAction(scope, $wrapper);
                    }
                }
            });
        },
        pullDownAction: function (scope, $wrapper) {
            if(scope.action) { // a valid scroll from
                scope.$emit(scope.vo.code + '_' + scope.action);
            }

            var $pullDown = $wrapper.find('.pulldown');
            setTimeout(function() {
                $wrapper.removeClass('pulldownrefresh');
                $pullDown.removeClass('flip loading').find('.label').html('Pull down to refresh...');
                scope.myScroll.refresh();
            }, 1000);
        },
        pullUpAction: function (scope, $wrapper) {
            if(scope.action) { // a valid scroll from
                scope.$emit(scope.vo.code + '_' + scope.action);
            }

            var $pullUp = $wrapper.find('.pullup');
            setTimeout(function() {
                $pullUp.removeClass('flip loading').find('.label').html('Pull up to load more...');
                scope.myScroll.refresh();
            }, 1000);
        },
        noMoreOrNot: function(scope, element) {
            var $pullUp = $(element).find('.pullup');
            if (scope.noMore) {
                $pullUp.hide();
            } else {
                $pullUp.show();
            }
        }
    }
}]);
